package com.example.hcd.functionactivity;

import java.util.ArrayList;

import com.example.hcd.R;
import com.example.hcd.adapt.AddressAdapt;
import com.example.hcd.datamanage.UserInfoData;
import com.example.hcd.internet.InternetKeep;
import com.example.hcd.tool.ToastHelp;

import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class AddressBookActivity extends 	BaseActivity implements OnItemClickListener{
	private View main_view;
	
	private AddressAdapt adapt;
	
	private ArrayList<UserInfoData> userList;
	
	private ListView list;

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		mPageName = "AddressBookActivity";
		LayoutInflater inflate = LayoutInflater.from(this);
		main_view = inflate.inflate(R.layout.address_book_view, null);
		setContentView(main_view);
		
		list = (ListView)main_view.findViewById(R.id.list);
		userList = InternetKeep.getInstence().getUserinfolist();

		adapt = new AddressAdapt(this);
		if(!InternetKeep.getInstence().isSystemInfoLoad||null == userList||userList.size() ==0)
		{
			InternetKeep.getInstence().getSystemInfo();
			finish();
			ToastHelp.doToast(this,"系统信息加载中,请稍后...");
		}else
		{
			adapt.setList(userList);
		}
		


		list.setAdapter(adapt);
		list.setOnItemClickListener(this);
		
		EditText search = (EditText)main_view.findViewById(R.id.searchbox);
		search.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
				// TODO Auto-generated method stub
				adapt.getFilter().filter(arg0); //这里传入数据就可以了
			}
			
			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void afterTextChanged(Editable arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
		
		// 返回键
		ImageView backBtn = (ImageView) findViewById(R.id.title_bar_btn_left);
		backBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		
	}
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) 
	{
		final UserInfoData  userinfoCatch = adapt.getItem(arg2);
		final AlertDialog roleDia = new AlertDialog.Builder(AddressBookActivity.this).create();
		roleDia.show();
		//View alertView = inflater.inflate(R.layout.choose_more_view, null);
		roleDia.getWindow().setContentView(R.layout.address_book_choose_view);
		
		TextView title = (TextView)roleDia.getWindow().findViewById(R.id.title);
		title.setText("你选择了"+userinfoCatch.name);
		
		TextView cancelBtn = (TextView)roleDia.getWindow().findViewById(R.id.cancel_btn);
		cancelBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				roleDia.dismiss();
			}
		});
		
		TextView phoneCallBtn = (TextView)roleDia.getWindow().findViewById(R.id.call_phone);
		phoneCallBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				String number = userinfoCatch.mobileNumber;  
                //用intent启动拨打电话  
                Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+number));  
                startActivity(intent); 

				roleDia.dismiss();
			}
		});
		
		TextView sendMsgBtn = (TextView)roleDia.getWindow().findViewById(R.id.send_msg);
		sendMsgBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {

				String number = userinfoCatch.mobileNumber;
				Uri uri = Uri.parse("smsto:" + number);
			    Intent sendIntent = new Intent(Intent.ACTION_VIEW, uri);
			    startActivity(sendIntent);
				
				roleDia.dismiss();
			}
		});
		
		TextView addToBook = (TextView)roleDia.getWindow().findViewById(R.id.add_address);
		addToBook.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				
				if(insert(userinfoCatch.name,userinfoCatch.mobileNumber,userinfoCatch.email))
				{
					Toast.makeText(AddressBookActivity.this, "添加联系人成功", Toast.LENGTH_SHORT).show();
				}else
				{
					ToastHelp.doToast(AddressBookActivity.this, "添加失败");
				}
				
				roleDia.dismiss();
			}
		});
		
		
	}
	
	public boolean insert(String given_name, String mobile_number,  
            String work_email) {  
        try {  
        	/* 往 raw_contacts 中添加数据，并获取添加的id号*/  
	        Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");  
	        ContentResolver resolver = getContentResolver();  
	        ContentValues values = new ContentValues();  
	        long contactId = ContentUris.parseId(resolver.insert(uri, values));  
	          
	        /* 往 data 中添加数据（要根据前面获取的id号） */  
	        // 添加姓名  
	        uri = Uri.parse("content://com.android.contacts/data");  
	        values.put("raw_contact_id", contactId);  
	        values.put("mimetype", "vnd.android.cursor.item/name");  
	        values.put("data2", given_name);  
	        resolver.insert(uri, values);  
	          
	        // 添加电话  
	        values.clear();  
	        values.put("raw_contact_id", contactId);  
	        values.put("mimetype", "vnd.android.cursor.item/phone_v2");  
	        values.put("data2", "2");  
	        values.put("data1", mobile_number);  
	        resolver.insert(uri, values);  
	          
	        // 添加Email  
	        values.clear();  
	        values.put("raw_contact_id", contactId);  
	        values.put("mimetype", "vnd.android.cursor.item/email_v2");  
	        values.put("data2", "2");  
	        values.put("data1", work_email);  
	        resolver.insert(uri, values);  
			
			//Toast.makeText(AddressBookActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
			
        }  
  
        catch (Exception e) {  
            return false;  
        }  
        return true;  
    } 
}
